﻿namespace TextEditor
{
    using System;
    using System.Collections;
    using System.Drawing;

    public class ZYTextBlock : ZYTextContainer
    {
        public bool bolStandOutBack = false;

        public override void AddElementToList(ArrayList myList, bool ResetFlag)
        {
            if (ResetFlag)
            {
                base.Visible = false;
                base.Index = -1;
            }
            if (base.myOwnerDocument.isVisible(this))
            {
                base.Visible = true;
                myList.Add(this);
                base.AddElementToList(myList, ResetFlag);
            }
        }

        public override Rectangle GetContentBounds()
        {
            Rectangle rect = Rectangle.Empty;
            foreach (ZYTextElement myElement in base.myChildElements)
            {
                if (rect.IsEmpty)
                {
                    rect = this.Bounds;
                }
                else
                {
                    rect = Rectangle.Union(rect, myElement.Bounds);
                }
            }
            return Rectangle.Union(rect, this.Bounds);
        }

        public override void HandleEnter()
        {
            this.RefreshForSelect();
        }

        public override void HandleLeave()
        {
            this.RefreshForSelect();
        }

        public override bool isNewLine()
        {
            return false;
        }

        public override bool isNewParagraph()
        {
            return false;
        }

        public override bool isTextElement()
        {
            return true;
        }

        public override bool OwnerWholeLine()
        {
            return false;
        }

        private void RefreshForSelect()
        {
            bool bolBack = this.bolStandOutBack;
            this.ResetViewState();
            if (bolBack != this.bolStandOutBack)
            {
                base.myOwnerDocument.RefreshElement(this);
            }
        }

        public override bool RefreshView()
        {
            this.DrawBackGround(this);
            return true;
        }

        public override void ResetViewState()
        {
            this.bolStandOutBack = false;
            if ((base.myOwnerDocument.Content.SelectLength == 0) && (((base.myOwnerDocument.Content.CurrentElement == this) || base.myChildElements.Contains(base.myOwnerDocument.Content.CurrentElement)) && (base.myOwnerDocument.CurrentHoverElement == this)))
            {
                this.bolStandOutBack = true;
            }
        }

        public override bool Block
        {
            get
            {
                return true;
            }
        }

        public bool KeyField
        {
            get
            {
                return (base.myAttributes.GetString("keyfield") != "0");
            }
            set
            {
                base.myAttributes.SetValue("keyfield", value ? "1" : "0");
            }
        }

        public override bool Locked
        {
            get
            {
                if (base.myParent == null)
                {
                    return false;
                }
                else
                {
                    return base.myParent.Locked;
                }
            }
            set
            {
                base.Locked = value;
            }
        }      
    }
}

